package com.sg.service.base.app.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.app.req.*;
import com.sg.entity.OmsEvaluationTemplate;
import com.sg.service.base.app.MOmsEvaluationTemplateService;
import com.wicket.okrapp.common.dto.FieldInfo;
import com.wicket.okrapp.common.exception.BizException;
import com.wicket.okrapp.dal.mapper.mbg.OmsEvaluationTemplateMapper;
import com.wicket.okrframework.common.annotations.BanAuto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsEvaluationTemplateServiceImpl
        extends ServiceImpl<OmsEvaluationTemplateMapper, OmsEvaluationTemplate>
        implements MOmsEvaluationTemplateService {

    @Autowired
    OmsEvaluationTemplateMapper omsEvaluationTemplateMapper;

    /**
     * 3-2-02修改评价模板[2393]
     * gen by moon at 10/15/2022, 9:59:36 PM
     */
    @Trace(operationName = "3-2-02修改评价模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateEvaTemp(OmsEvaluationTemplate omsEvaluationTemplate) {
        omsEvaluationTemplate.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationTemplate.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        return super.updateById(omsEvaluationTemplate);
    }

    /**
     * 3-2-02新增评价模板[2344]
     * gen by moon at 10/15/2022, 9:59:36 PM
     */
    @Trace(operationName = "3-2-02新增评价模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addEvaTemp(OmsEvaluationTemplate omsEvaluationTemplate) {
        omsEvaluationTemplate.setEvaluationTemplateId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsEvaluationTemplate.setEvaluationTemplateCode(CommonFunctionHelper.getUid());//AUTO_GENERATE
        omsEvaluationTemplate.setBatchEndtTime(CommonFunctionHelper.getFutureTime());//FUTURE_CURRENT_TIME
        omsEvaluationTemplate.setIsUpdate("TRUE");
        omsEvaluationTemplate.setUpdateStatus("NEW");
        omsEvaluationTemplate.setArchivngType("NOT_ARCHIVE");
        omsEvaluationTemplate.setIsEditing("TRUE");
        omsEvaluationTemplate.setIsArchive("FALSE");
        omsEvaluationTemplate.setSubjectLifeCycle("EDITING");
        omsEvaluationTemplate.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationTemplate.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationTemplate.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationTemplate.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationTemplate.setIsValid("FALSE");
        omsEvaluationTemplate.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsEvaluationTemplate.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsEvaluationTemplate)) {
            return omsEvaluationTemplate.getEvaluationTemplateId();
        } else {
            return "-1";
        }

    }

    /**
     * 3-2-02查评价模板详情[2316]
     * gen by moon at 3/30/2023, 12:38:45 PM
     */
    @Trace(operationName = "3-2-02查评价模板详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationTemplate queryEvaTempDetail(QueryEvaTempDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            reqDto.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
            QueryWrapper<OmsEvaluationTemplate> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaluationTemplate> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsEvaluationTemplate retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-03批量查评价模板列表[2192]
     * gen by moon at 6/29/2023, 10:16:49 PM
     */
    @Trace(operationName = "3-2-03批量查评价模板列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationTemplate> batchQueryEvaTempList(BatchQueryEvaTempListReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationTemplate> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getEvalTempList() == null || reqDto.getEvalTempList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("evaluation_template_id", reqDto.getEvalTempList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationTemplate.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationTemplate.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationTemplate.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("create_time");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaluationTemplate>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-02查评价模板列表[2191]
     * gen by moon at 10/15/2022, 9:59:33 PM
     */
    @Trace(operationName = "3-2-02查评价模板列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationTemplate> queryEvaTempList(QueryEvaTempListReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationTemplate> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationTemplate.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationTemplate.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationTemplate.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsEvaluationTemplate>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-02批量查询评价模板[3075]
     * gen by moon at 10/15/2022, 9:59:37 PM
     */
    @Trace(operationName = "3-2-02批量查询评价模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsEvaluationTemplate> batchQueryEvaTemp(BatchQueryEvaTempReq reqDto) {

        try {
            QueryWrapper<OmsEvaluationTemplate> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getEvalTempList() == null || reqDto.getEvalTempList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("evaluation_template_id", reqDto.getEvalTempList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsEvaluationTemplate.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsEvaluationTemplate.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsEvaluationTemplate.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsEvaluationTemplate>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M3-2-02构造单条评价模板验证数据[4265]
     * gen by moon at 10/15/2022, 9:59:38 PM
     */
    @Trace(operationName = "M3-2-02构造单条评价模板验证数据")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String buildSingleEvaTemplate(OmsEvaluationTemplate omsEvaluationTemplate) {
        omsEvaluationTemplate.setEvaluationTemplateId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsEvaluationTemplate.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsEvaluationTemplate.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsEvaluationTemplate != null && omsEvaluationTemplate.getSubjectLifeCycle() != null && omsEvaluationTemplate.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationTemplate.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsEvaluationTemplate.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsEvaluationTemplate.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsEvaluationTemplate != null && omsEvaluationTemplate.getSubjectLifeCycle() != null && omsEvaluationTemplate.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsEvaluationTemplate.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsEvaluationTemplate.setIsValid("FALSE");
        omsEvaluationTemplate.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsEvaluationTemplate.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsEvaluationTemplate)) {
            return omsEvaluationTemplate.getEvaluationTemplateId();
        } else {
            return "-1";
        }

    }

    /**
     * M3-2-02修改单条评价模板[4283]
     * gen by moon at 10/15/2022, 9:59:39 PM
     */
    @Trace(operationName = "M3-2-02修改单条评价模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateSingleEvaTemplate(OmsEvaluationTemplate omsEvaluationTemplate) {

        return super.updateById(omsEvaluationTemplate);
    }

    /**
     * M3查询单条评价模板详情[4303]
     * gen by moon at 3/30/2023, 12:38:46 PM
     */
    @Trace(operationName = "M3查询单条评价模板详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationTemplate querySingleEvaTemplateDetail(QuerySingleEvaTemplateDetailReq reqDto) {

        try {

            QueryWrapper<OmsEvaluationTemplate> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaluationTemplate> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsEvaluationTemplate retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M3-删除单条评价模板[4363]
     * gen by moon at 10/15/2022, 9:59:40 PM
     */
    @Trace(operationName = "M3-删除单条评价模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteSingleEvaTemplate(String evaluationTemplateId) {

        return super.removeById(evaluationTemplateId);
    }

    /**
     * 3-2-02查询新增的评价模板[4551]
     * gen by moon at 3/30/2023, 12:38:46 PM
     */
    @Trace(operationName = "3-2-02查询新增的评价模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationTemplate queryAddEvaTempDetail(QueryAddEvaTempDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            reqDto.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
            QueryWrapper<OmsEvaluationTemplate> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsEvaluationTemplate> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsEvaluationTemplate retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 3-2-02删除评价模板[4764]
     * gen by moon at 10/15/2022, 9:59:41 PM
     */
    @Trace(operationName = "3-2-02删除评价模板")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteEvaTemp(String evaluationTemplateId) {

        return super.removeById(evaluationTemplateId);
    }

    /**
     * 3-3-09悬停查评价模板详情[4846]
     * gen by moon at 10/14/2022, 10:51:48 PM
     */
    @Trace(operationName = "3-3-09悬停查评价模板详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @BanAuto
    @Transactional(rollbackFor = Exception.class)
    public OmsEvaluationTemplate queryHoverEvaTempDetail(QueryHoverEvaTempDetailReq reqDto) {
        try {
            long count = 500;
            while (true) {
                if (count == 0) {
                    break;
                }
                reqDto.setIsValid("FALSE");
                QueryWrapper<OmsEvaluationTemplate> queryWrapper = new QueryWrapper<>();
                Map<String, Object> fieldInfoMap = null;
                fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
                CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
                queryWrapper.orderByDesc("create_time");
                if (super.list(queryWrapper) != null && super.list(queryWrapper).size() > 0) {
                    return super.list(queryWrapper).get(0);
                }
                count--;
                Thread.sleep(10);
            }
            throw new BizException("-1", false);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }

    }
}
